//
//  SpecifyDoctorManager.m
//  安好
//
//  Created by qinlongqing on 14/12/6.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SpecifyDoctorManager.h"
#import "AppConfig.h"
#import "CLDataBaseController.h"

#define kIsFinishedLoadSpecifyDoctorFromNetwork   @"kIsFinishedLoadSpecifyDoctorFromNetwork"

static SpecifyDoctorManager *instance = nil;

@interface SpecifyDoctorManager ()
{
}
@end

@implementation SpecifyDoctorManager

+ (SpecifyDoctorManager *)instance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

//- (void)setDelegate:(id<SpecifyDoctorManagerDelegate>)delegate{
//    _delegate = delegate;
//    
//    [instance getAllSpecifyDoctors];//获取所有的指定医生
//}

- (void)getAllSpecifyDoctors{
    BOOL isFinishedLoadSpecifyDoctorFromNetwork = [[[NSUserDefaults standardUserDefaults] valueForKey:kIsFinishedLoadSpecifyDoctorFromNetwork] boolValue];
    if (isFinishedLoadSpecifyDoctorFromNetwork == NO) {//还没从网上下载过指定医生
        [self getAllSpecifyDoctorsFromNetwork];
    }
    else{
        if (self.delegate && [self.delegate respondsToSelector:@selector(didLoadAllSpecifyDoctors:)]) {
            [self.delegate didLoadAllSpecifyDoctors:[self getAllSpecifyDoctorsFromDB]];
        }
    }
}

- (void)getAllSpecifyDoctorsFromNetwork{//获取指定医生列表
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion};
    [[AFHTTPRequestOperationManager manager] POST:[NSString stringWithFormat:@"%@%@",kBaseURL,kGetSpecifyDoctorsURL] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {

        }
        else{
            [self storeSpecifyDoctorsToLocal:[responseObject objectForKey:@"data"]];
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kIsFinishedLoadSpecifyDoctorFromNetwork];
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(didLoadAllSpecifyDoctors:)]) {
            [self.delegate didLoadAllSpecifyDoctors:[responseObject objectForKey:@"data"]];
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)storeSpecifyDoctorsToLocal:(NSArray *)allSpecifyDoctorsFromNetwork{//第一次从服务器上加载所有的指定医生
    for (NSInteger i=0; i<allSpecifyDoctorsFromNetwork.count; i++) {
        [[CLDataBaseController instance] insertSpecifyDoctorWithDict:[allSpecifyDoctorsFromNetwork objectAtIndex:i]];
    }
}

- (NSArray *)getAllSpecifyDoctorsFromDB{//得到所有指定的医生
    return [[CLDataBaseController instance] selectAllSpecifyDoctor];
}

- (void)insertSpecifyDoctorToDB:(NSDictionary *)specifyDoctorDict{
    BOOL isFinishedLoadSpecifyDoctorFromNetwork = [[[NSUserDefaults standardUserDefaults] valueForKey:kIsFinishedLoadSpecifyDoctorFromNetwork] boolValue];
    if (isFinishedLoadSpecifyDoctorFromNetwork == YES) {//从网络上加载完以后才能插入，这样就保持客户端和服务器端一致
        [[CLDataBaseController instance] insertSpecifyDoctorWithDict:specifyDoctorDict];
    }
}

- (void)deleteSpecifyDoctorFromDBWithDoctoruid:(NSString *)doctoruid{
    BOOL isFinishedLoadSpecifyDoctorFromNetwork = [[[NSUserDefaults standardUserDefaults] valueForKey:kIsFinishedLoadSpecifyDoctorFromNetwork] boolValue];
    if (isFinishedLoadSpecifyDoctorFromNetwork == YES) {//从网络上加载完以后才能插入，这样就保持客户端和服务器端一致
        [[CLDataBaseController instance] deleteSpecifyDoctorWithUID:doctoruid];
    } 
}

@end
